package com.aspose.cells;

import java.util.ArrayList;
import java.util.Collections;

public class CommentCollection extends CollectionBase {
   private Comment b;
   private Worksheet c;

   CommentCollection(Worksheet sheet) {
      this.c = sheet;
      this.b = new Comment();
   }

   Worksheet a() {
      return this.c;
   }

   public int add(int row, int column) {
      zakq.a(row, column);
      int var3 = this.a(row, column);
      if (var3 > -1) {
         return var3;
      } else {
         var3 = -var3 - 1;
         Comment var4 = new Comment(this, row, column);
         var4.c();
         this.a.add(var3, var4);
         this.c.getShapes().c(var4.getCommentShape());
         return var3;
      }
   }

   public int add(String cellName) {
      byte var2 = 0;
      byte var3 = 0;
      int[] var4 = new int[]{var2};
      int[] var5 = new int[]{var3};
      CellsHelper.a(cellName, var4, var5);
      int var6 = var4[0];
      int var7 = var5[0];
      return this.add(var6, var7);
   }

   int a(String var1) {
      byte var2 = 0;
      byte var3 = 0;
      int[] var4 = new int[]{var2};
      int[] var5 = new int[]{var3};
      CellsHelper.a(var1, var4, var5);
      int var6 = var4[0];
      int var7 = var5[0];
      return this.add(var6, var7);
   }

   public Comment get(int index) {
      return (Comment)this.a.get(index);
   }

   public Comment get(String cellName) {
      byte var2 = 0;
      byte var3 = 0;
      int[] var4 = new int[]{var2};
      int[] var5 = new int[]{var3};
      CellsHelper.a(cellName, var4, var5);
      int var7 = var4[0];
      int var8 = var5[0];
      int var6 = this.a(var7, var8);
      return var6 < 0 ? null : (Comment)this.a.get(var6);
   }

   public Comment get(int row, int column) {
      zakq.a(row, column);
      int var3 = this.a(row, column);
      return var3 < 0 ? null : (Comment)this.a.get(var3);
   }

   int a(int var1, int var2) {
      this.b.a(var1);
      this.b.b(var2);
      return Collections.binarySearch(this.a, this.b, zakq.a());
   }

   void a(Comment var1) {
      int var2 = Collections.binarySearch(this.a, var1, zakq.a());
      if (var2 < 0) {
         var2 = -var2 - 1;
         this.a.add(var2, var1);
      }

   }

   void a(CellArea var1, int var2, int var3) {
      boolean var4 = false;

      for(int var5 = this.getCount() - 1; var5 > -1; --var5) {
         Comment var6 = (Comment)this.a.get(var5);
         if (var1.a(var6.getRow(), var6.getColumn())) {
            int var7 = var6.getRow();
            int var8 = var6.getColumn();
            var6.a(var7 + var2);
            var6.b(var8 + var3);
            var6.getCommentShape().setUpperLeftRow(var6.getCommentShape().getUpperLeftRow() + var2);
            var6.getCommentShape().setUpperLeftColumn(var6.getCommentShape().getUpperLeftColumn() + var3);
            var4 = true;
         }
      }

      if (var4) {
         Collections.sort(this.a, zakq.a());
      }

   }

   void b(Comment var1) {
      this.a.remove(var1);
   }

   public void removeAt(String cellName) {
      byte var2 = 0;
      byte var3 = 0;
      int[] var4 = new int[]{var2};
      int[] var5 = new int[]{var3};
      CellsHelper.a(cellName, var4, var5);
      int var6 = var4[0];
      int var7 = var5[0];
      this.removeAt(var6, var7);
   }

   public void removeAt(int row, int column) {
      int var3 = this.a(row, column);
      if (var3 >= 0) {
         Comment var4 = (Comment)this.a.get(var3);
         this.a.remove(var3);
         ShapeCollection var5 = this.c.getShapes();
         var5.d(var4.getCommentShape().x());
      }
   }

   public void clear() {
      this.a.clear();
      ShapeCollection var1 = this.c.getShapes();

      for(int var2 = var1.getCount() - 1; var2 >= 0; --var2) {
         if (var1.get(var2).getMsoDrawingType() == 25) {
            var1.d(var2);
         }
      }

   }

   void a(Range var1, Range var2, boolean var3) {
      for(int var4 = this.getCount() - 1; var4 > -1; --var4) {
         Comment var5 = this.get(var4);
         if (var2.a().a(var5.getRow(), var5.getColumn())) {
            this.a().getShapes().b(var5.getCommentShape());
         }
      }

      ArrayList var11 = var1.getWorksheet().getComments().a;
      if (var11.size() >= 1) {
         if (var1.getWorksheet().getComments() == this) {
            var11 = new ArrayList(var11);
         }

         int var6;
         int var7;
         int var12;
         if (var3) {
            var12 = var1.a().StartRow;
            var6 = var1.a().StartColumn;
            var7 = var2.a().StartRow;
            int var8 = var2.a().StartColumn;

            for(int var9 = var11.size() - 1; var9 > -1; --var9) {
               Comment var10 = (Comment)var11.get(var9);
               if (var1.a().a(var10.getRow(), var10.getColumn())) {
                  this.get(this.add(var7 + var10.getColumn() - var6, var8 + var10.getRow() - var12)).a(var10);
               }
            }
         } else {
            var12 = var2.a().StartRow - var1.a().StartRow;
            var6 = var2.a().StartColumn - var1.a().StartColumn;

            for(var7 = var11.size() - 1; var7 > -1; --var7) {
               Comment var13 = (Comment)var11.get(var7);
               if (var1.a().a(var13.getRow(), var13.getColumn())) {
                  this.get(this.add(var13.getRow() + var12, var13.getColumn() + var6)).a(var13);
               }
            }
         }

      }
   }

   void a(CommentCollection var1, int var2, int var3, int var4) {
      int var5 = var2 + var4 - 1;
      int var6 = var3 + var4 - 1;

      for(int var7 = this.getCount() - 1; var7 > -1; --var7) {
         Comment var8 = (Comment)this.a.get(var7);
         if (var8.getColumn() >= var3 && var8.getColumn() <= var6) {
            this.c.getShapes().b(var8.getCommentShape());
         }
      }

      if (var1.getCount() >= 1) {
         ArrayList var11 = var1.a;
         if (var1 == this) {
            var11 = new ArrayList(var11);
         }

         int var12 = var3 - var2;

         for(int var9 = var11.size() - 1; var9 > -1; --var9) {
            Comment var10 = (Comment)var11.get(var9);
            if (var10.getColumn() >= var2 && var10.getColumn() <= var5) {
               this.get(this.add(var10.getRow(), var10.getColumn() + var12)).b().a(var10.b());
            }
         }

      }
   }

   void b(int var1, int var2) {
      if (var2 != 0) {
         for(int var3 = 0; var3 < this.getCount(); ++var3) {
            Comment var4 = this.get(var3);
            if (var1 <= var4.getRow()) {
               if (var2 < 0 && var4.getRow() >= var1 && var4.getRow() < var1 - var2) {
                  this.removeAt(var3);
                  this.c.getShapes().b(var4.getCommentShape());
                  --var3;
               } else {
                  if (var4.getRow() + var2 > 1048575) {
                     throw new CellsException(10, "Aspose.Cells cannot shift nonblank cell off the worksheet.");
                  }

                  var4.a(var4.getRow() + var2);
                  if (var4.getCommentShape().getPlacement() == 0) {
                     int var5 = var4.getCommentShape().c(var1, 0, var2 < 0 ? var1 - var2 : var1 + var2, 0);
                     if (var2 < 0) {
                        var4.getCommentShape().setTopToCorner(var4.getCommentShape().getTopToCorner() - var5);
                     } else {
                        var4.getCommentShape().setTopToCorner(var4.getCommentShape().getTopToCorner() + var5);
                     }
                  }
               }
            }
         }

      }
   }

   void c(int var1, int var2) {
      if (var2 != 0) {
         for(int var3 = 0; var3 < this.getCount(); ++var3) {
            Comment var4 = this.get(var3);
            if (var1 <= var4.getColumn()) {
               if (var2 < 0 && var1 - var2 > var4.getColumn()) {
                  this.removeAt(var3);
                  this.c.getShapes().b(var4.getCommentShape());
                  --var3;
               }

               if (var4.getColumn() + var2 > 16383) {
                  throw new CellsException(10, "Aspose.Cells cannot shift nonblank cell off the worksheet.");
               }

               var4.b(var4.getColumn() + var2);
               if (var4.getCommentShape().getPlacement() == 0) {
                  int var5 = var4.getCommentShape().d(var1, 0, var2 < 0 ? var1 - var2 : var1 + var2, 0);
                  if (var2 < 0) {
                     var4.getCommentShape().setLeftToCorner(var4.getCommentShape().getLeftToCorner() - var5);
                  } else {
                     var4.getCommentShape().setLeftToCorner(var4.getCommentShape().getLeftToCorner() + var5);
                  }
               }
            }
         }

      }
   }

   void b(CellArea var1, int var2, int var3) {
      switch(var3) {
      case 0:
         this.f(var1, var2, var3);
         break;
      case 1:
         this.c(var1, var2, var3);
      case 2:
      default:
         break;
      case 3:
         this.e(var1, var2, var3);
         break;
      case 4:
         this.d(var1, var2, var3);
      }

   }

   private void c(CellArea var1, int var2, int var3) {
      for(int var4 = 0; var4 < this.getCount(); ++var4) {
         Comment var5 = this.get(var4);
         int var6 = var5.getRow();
         int var7 = var5.getColumn();
         if (var6 >= var1.StartRow && var6 <= var1.EndRow && var7 >= var1.StartColumn) {
            if (var7 <= var1.EndColumn) {
               this.c.getShapes().b(var5.getCommentShape());
               --var4;
            } else {
               var5.b(var5.getColumn() - var2);
               int var8 = var5.getCommentShape().getUpperLeftColumn() - var2;
               if (var8 < 0) {
                  var8 = 0;
               }

               var5.getCommentShape().setUpperLeftColumn(var8);
            }
         }
      }

   }

   private void d(CellArea var1, int var2, int var3) {
      boolean var4 = false;

      for(int var5 = 0; var5 < this.getCount(); ++var5) {
         Comment var6 = this.get(var5);
         int var7 = var6.getRow();
         int var8 = var6.getColumn();
         if (var8 >= var1.StartColumn && var8 <= var1.EndColumn && var7 >= var1.StartRow) {
            if (var7 <= var1.EndRow) {
               this.c.getShapes().b(var6.getCommentShape());
               --var5;
            } else {
               var6.a(var7 - var2);
               int var9 = var6.getCommentShape().getUpperLeftRow() - var2;
               if (var9 < 0) {
                  var9 = 0;
               }

               var6.getCommentShape().setUpperLeftRow(var9);
               var4 = true;
            }
         }
      }

      if (var4) {
         Collections.sort(this.a, zakq.a());
      }

   }

   private void e(CellArea var1, int var2, int var3) {
      for(int var4 = 0; var4 < this.getCount(); ++var4) {
         Comment var5 = this.get(var4);
         if (var5.getRow() >= var1.StartRow && var5.getRow() <= var1.EndRow && var5.getColumn() >= var1.StartColumn) {
            var5.b(var5.getColumn() + var2);
            if (var5.getCommentShape().getPlacement() == 0) {
               int var6 = var5.getCommentShape().d(var1.StartColumn, 0, var1.StartColumn + var2, 0);
               var5.getCommentShape().setLeftToCorner(var5.getCommentShape().getLeftToCorner() + var6);
            }
         }
      }

   }

   private void f(CellArea var1, int var2, int var3) {
      boolean var4 = false;

      for(int var5 = 0; var5 < this.getCount(); ++var5) {
         Comment var6 = this.get(var5);
         if (var6.getRow() >= var1.StartRow && var6.getColumn() >= var1.StartColumn && var6.getColumn() <= var1.EndColumn) {
            var6.a(var6.getRow() + var2);
            if (var6.getCommentShape().getPlacement() == 0) {
               int var7 = var6.getCommentShape().c(var1.StartRow, 0, var1.StartRow + var2, 0);
               var6.getCommentShape().setTopToCorner(var6.getCommentShape().getTopToCorner() + var7);
            }

            var4 = true;
         }
      }

      if (var4) {
         Collections.sort(this.a, zakq.a());
      }

   }
}
